package com.voltmao.controller.module.corp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.persistent.model.CorpOrganization;
import com.voltmao.service.application.corp.CorpOrganizationServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.PropertiesUtils;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 企业组织控制层
 *
 * @author zhanglin
 * @date 2018/04/06
 */
@Controller
@RequestMapping(value = "/module/sys/corp_org")
public class CorpOrganizationController {
    @Autowired
    CorpOrganizationServiceImpl corpOrganizationService;

    @ResponseBody
    @RequestMapping(value = "/apiInsertOrUpdate.json", method = RequestMethod.POST)
    public ApiResult testOrg(HttpServletRequest request) {
        /*
         * 新增或删除企业组织信息
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult)->{
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            System.out.println(params.toString());

            CorpOrganization corpOrg = (CorpOrganization)PropertiesUtils.mapToObject(params, CorpOrganization.class);

            int flag = corpOrganizationService.insertOrUpdateOrgByObject(corpOrg);
            if (flag == 0) {
                apiResult.msg = "新增或修改失败";
                apiResult.status = Constants.AUTH_FAIVAL.toString();
            } else {
                apiResult.msg = "新增或修改成功！";
                apiResult.status = Constants.SUCCESS.toString();
            }

            return flag;
        });
    }

    @ResponseBody
    @RequestMapping(value = "/apiOrgs.json", method = RequestMethod.POST)
    public ApiResult getByPK(HttpServletRequest request) {

        return new DTOSmith(request).builderApiResult((request1, apiResult)->{
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            apiResult.setStatus(Constants.SUCCESS);
            //返回所有列表
            return corpOrganizationService.selectOrgsDataPage(params);
        });
    }

    @ResponseBody
    @RequestMapping(value = "/gridList.json", method = RequestMethod.POST)
    public GridViewResult testOrgGrid(HttpServletRequest request) {

        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                Map<String, Object> params = new HashMap(1);
                params.put("pageNumber",1);
                params.put("pageSize",20);
                return corpOrganizationService.selectOrgsDataPage(params);
            }

            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/module/sys/corp_org/gridList.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                //抛出DATAURL异常
                return corpOrganizationService.selectOrgsDataPage(params);
            }
        });
    }


}
